package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.EpmInformation;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EpmInformationResignationRecordMapper;
import com.ruoyi.system.domain.EpmInformationResignationRecord;
import com.ruoyi.system.service.IEpmInformationResignationRecordService;
import java.util.Date;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.system.domain.SysPost;
/**
 * 离职记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@Service
public class EpmInformationResignationRecordServiceImpl implements IEpmInformationResignationRecordService 
{
    @Autowired
    private EpmInformationResignationRecordMapper epmInformationResignationRecordMapper;

    @Autowired
    private ISysPostService sysPostService;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询离职记录
     * 
     * @param id 离职记录主键
     * @return 离职记录
     */
    @Override
    public EpmInformationResignationRecord selectEpmInformationResignationRecordById(Long id)
    {
        return epmInformationResignationRecordMapper.selectEpmInformationResignationRecordById(id);
    }

    /**
     * 查询离职记录列表
     * 
     * @param epmInformationResignationRecord 离职记录
     * @return 离职记录
     */
    @Override
    public List<EpmInformationResignationRecord> selectEpmInformationResignationRecordList(EpmInformationResignationRecord epmInformationResignationRecord)
    {
        return epmInformationResignationRecordMapper.selectEpmInformationResignationRecordList(epmInformationResignationRecord);
    }

    /**
     * 新增离职记录
     * 
     * @param epmInformationResignationRecord 离职记录
     * @return 结果
     */
    @Override
    public int insertEpmInformationResignationRecord(EpmInformationResignationRecord epmInformationResignationRecord)
    {
        return epmInformationResignationRecordMapper.insertEpmInformationResignationRecord(epmInformationResignationRecord);
    }

    /**
     * 修改离职记录
     * 
     * @param epmInformationResignationRecord 离职记录
     * @return 结果
     */
    @Override
    public int updateEpmInformationResignationRecord(EpmInformationResignationRecord epmInformationResignationRecord)
    {
        return epmInformationResignationRecordMapper.updateEpmInformationResignationRecord(epmInformationResignationRecord);
    }

    /**
     * 批量删除离职记录
     * 
     * @param ids 需要删除的离职记录主键
     * @return 结果
     */
    @Override
    public int deleteEpmInformationResignationRecordByIds(Long[] ids)
    {
        return epmInformationResignationRecordMapper.deleteEpmInformationResignationRecordByIds(ids);
    }

    /**
     * 删除离职记录信息
     * 
     * @param id 离职记录主键
     * @return 结果
     */
    @Override
    public int deleteEpmInformationResignationRecordById(Long id)
    {
        return epmInformationResignationRecordMapper.deleteEpmInformationResignationRecordById(id);
    }



    /**
     * 把员工信息实体的信息赋值给离职记录实体
     *
     * @param epmInformationResignationRecord,EpmInformation 离职记录
     * @return 结果
     */
    @Override
    public EpmInformationResignationRecord useEpm_updateEpmInformationResignationRecord(EpmInformationResignationRecord epmInformationResignationRecord, EpmInformation epm)
    {
        epmInformationResignationRecord.setDeptName(epm.getDeptName());

        //记录post
        // 获取用户的岗位 ID 列表
        List<Long> postIds = sysPostService.selectPostListByUserId(epm.getUserId());
        // 用于存储查询结果的列表
        List<String> postNames = new ArrayList<>();
        // 检查 postIds 是否为 null
        if (postIds != null && !postIds.isEmpty()) {
            for (Long postId : postIds) {
                // 查询每个 postId 的 SysPost 对象
                SysPost post = sysPostService.selectPostById(postId);
                if (post != null) {
                    // 将 post 的名称（假设有一个 getPostName 方法）添加到列表中
                    postNames.add(post.getPostName());
                }
            }
        } else {
            System.out.println("没有找到与用户相关的岗位 ID，无法进行遍历。");
        }
        // 将所有结果拼接成一个字符串
        String allPostNames = String.join(", ", postNames);
        // 设置到 epmInformationResignationRecord 中
        epmInformationResignationRecord.setPost(allPostNames);


        epmInformationResignationRecord.setEntryDate(epm.getDate());
        Date currentDate = new Date();
        epmInformationResignationRecord.setResignationDate(currentDate);
        epmInformationResignationRecord.setIdCard(epm.getIdCard());
        epmInformationResignationRecord.setBirthday(epm.getBirthday());
        //todo age
        epmInformationResignationRecord.setSex(epm.getSex());
        epmInformationResignationRecord.setHighEducationalLevel(epm.getHighEducationalLevel());



        epmInformationResignationRecord.setPhoneNumber( epm.getPhoneNumber());

        //typeOfMen
        List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType("type_of_men");
        // 从 epm 中获取 TypeOfMen 的值，假设它的类型是 String
        String typeOfMen = epm.getTypeOfMen();
// 用于存储找到的 dictLabel
        String correspondingDictLabel = null;
// 遍历 dictDatas 列表，查找对应的实体
        for (SysDictData dictData : dictDatas) {
            // 检查 dictValue 是否与 typeOfMen 匹配
            if (dictData.getDictValue().equals(typeOfMen)) { // 直接比较 String 类型
                epmInformationResignationRecord.setTypeOfMen(dictData.getDictLabel());
                break; // 找到后可以跳出循环
            }
        }



        epmInformationResignationRecord.setDormitoryNeed(epm.getDormitoryNeed());

        return epmInformationResignationRecord;
    }


}
